let iapChannel = null
import { getUserInfo, getCombo } from '@/api/my.js'
import { httpTokenRequest } from '@/utils/http.js';
export default {
	data() {
		return {
			userInfo: {},//用户信息
			vipList: [{
				id: 1,
				name: '大会员',
				type: 1
			}, {
				id: 2,
				name: '超级会员',
				type: 2
			}, {
				id: 3,
				name: '企业会员',
				type: 3
			}],//会员类型
			current: 0,//当前选择的会员类型
			vipType: 1,//会员类型
			packageCurrent: 0,//当前选择的套餐
			setMealList: [],//会员类型套餐列表
			truePrice: '',//真实价格
			vipId: '',//会员套餐id
			detailType: '',//详情类型
			collegeId: '',//跳回小竹学院
			productIds: [],
		};
	},
	onLoad(e) {
		this.getUserInfo();
		this.getSetMealList();
	},
	methods: {
		//tab切换
		tabsClick(e) {
			this.vipType = e.type
			this.packageCurrent = 0
			this.getSetMealList()
		},

		//获取用户信息
		getUserInfo() {
			getUserInfo().then(res => {
				this.userInfo = res.data
			})
		},

		//获取会员套餐
		getSetMealList() {
			getCombo({ type: this.vipType }).then(res => {
				if (res.code == 1) {
					this.setMealList = res.data
					this.truePrice = res.data[0].ios_price
					this.vipId = res.data[0].id
					if (this.setMealList.length > 0) {
						this.packageCurrent = 0;
						this.setMealList.forEach(item => {
							this.productIds.push(item.ios_productid)
						})
						//检查苹果支付渠道
						// #ifdef APP-PLUS
						this.checkAppleChannel()
						// #endif
					} else {
						this.$used.appBack(msg)
					}
				} else {
					this.$used.appBack(msg)
				}
			})
		},

		//选择套餐
		chooseSetMeal(item, index) {
			this.truePrice = item.ios_price
			this.vipId = item.id
			if (this.packageCurrent != index) {
				this.packageCurrent = index
			}
		},

		//去兑换
		goExChange() {
			uni.navigateTo({
				url: `/pages/my/vip/exchange`
			})
		},


		/**
		 * 支付第一步:检查支付渠道
		 */
		checkAppleChannel() {
			uni.showLoading({
				title: '支付渠道检查...',
				mask: true
			})
			plus.payment.getChannels((channels) => {
				for (var i = 0; i < channels.length; i++) {
					var channel = channels[i];
					if (channel.id == 'appleiap') {
						iapChannel = channel;
						uni.hideLoading();
						this.requestOrder();
						break
					}
				}
				if (iapChannel == null) {
					this.errorMsg('')
				}
			}, (error) => {
				this.errorMsg('!')
			});
		},

		/**
		 * 支付第二步:检测支付环境
		 */
		requestOrder() {
			uni.showLoading({
				title: '检测支付环境...',
				mask: true
			})
			iapChannel.requestOrder(this.productIds, (orderList) => { //必须调用此方法才能进行 iap 支付
				uni.hideLoading();
			}, (e) => {
				this.errorMsg('。')
			});
		},


		//点击支付按钮
		payment() {
			uni.showLoading({
				title: '调起支付中...',
				mask: true
			})
			var data = {
				id: this.setMealList[this.packageCurrent].id,
			}
			httpTokenRequest({ method: 'POST', url: "/api/Iospay/vipcomboplace", params: data }).then(res => {
				if (res.code == 1) {
					uni.hideLoading();
					console.log("充值下单结果", res);
					this.requestPayment(res.data.order_sn); //发起支付动作
				} else {
					uni.hideLoading();
					this.$tools.showToast(res.msg);
				}
			})
				.catch(err => {
					uni.hideLoading();
					console.log('错误回调', err);
				});
		},


		/**
			 * 支付第三步:发起支付动作
			 */
		requestPayment(order_no) {
			var that = this;
			uni.showLoading({
				title: '支付中...',
				mask: true
			})
			uni.requestPayment({
				provider: 'appleiap',
				orderInfo: {
					productid: this.setMealList[this.packageCurrent].ios_productid,
					optimize: true,
					username: 'xiaozhu'
				},
				success: (e) => {
					uni.hideLoading();

					console.log(e, '支付完成，接下来调接口')
					// 这里把参数返回服务器进行二次验证
					var data = {
						receipt: e.transactionReceipt,
						order_no: order_no
					}
					httpTokenRequest({ method: 'POST', url: this.$paths.iosVipPays, params: data }) // 会员套餐：verification
						.then(res => {
							console.log(res);
							if (res.code == 1) {
								console.log("充值下单结果", res);
								that.$tools.showToast("购买成功");
								that.getCombo()
								that.getUserInfo()
							} else {
								console.log("充值下单失败结果", res.msg);
								that.$tools.showToast('充值下单失败');
							}
						})
						.catch(err => {
							uni.hideLoading();
							console.log('错误回调', err);
						});
				},
				fail: (e) => {
					uni.hideLoading();
					console.log("发起支付失败", e);
					uni.showModal({
						content: "取消支付",
						showCancel: false
					})
				},
			})
		}
	}

}